<?php

/*
 * $Author ：PHPYUN开发团队
 *
 * 官网: http://www.phpyun.com
 *
 * 版权所有 2009-2019 宿迁鑫潮信息技术有限公司，并保留所有权利。
 *
 * 软件声明：未经授权前提下，不得用于商业运营、二次开发以及任何形式的再次发布。
 */
class crm_index_controller extends adminCommon{
    
    function index_action() {
        
        $crmM   =   $this -> MODEL('crm');
        $orderM =   $this->MODEL('companyorder');

        $auid   =   intval($_SESSION['auid']);

        // 代办任务：今日和进入之前未完成任务
        $todayEnd = strtotime('tomorrow');
        $tWhere['uid']              =   $auid;
        $tWhere['PHPYUNBTWSTART_A'] =   '';
        $tWhere['stime'][]          =   array('isnull', '', 'OR');
        $tWhere['stime'][]          =   array('<', $todayEnd, 'OR');
        $tWhere['PHPYUNBTWEND_A']   =   '';
        $tWhere['status']           =   '1';
        $tWhere['orderby']          =   'stime';
        
        $tasks = $crmM->getTaskList($tWhere, array('utype' => 'crm'));
        $this -> yunset('tasks', $tasks);
		$this -> yunset('tasknum',count($tasks));
		
		// 待审核订单
		$orders  	=   $orderM -> getList(array('crm_uid' => $auid, 'order_state' => '1'),array('utype'=>'crmdealsp'));
		$this -> yunset('orders', $orders);
		$this -> yunset('orderNum',count($orders));
		
		//会员套餐
		$ratingM	=	$this -> MODEL('rating');
	    $ratinglist	=	$ratingM -> getList(array('category' => '1', 'service_price' => array('>', '0')), array('field'=>'`id`,`name`'));
		$this -> yunset('ratinglist', $ratinglist);
		
		$cacheM		=   $this -> MODEL('cache');  
        $cache		=   $cacheM -> GetCache(array('crm'));
        $this -> yunset('cache', $cache);
        $AdminM			=	$this -> MODEL('admin');
		$where=array();
		$adminUserList	=	$AdminM -> getList($where,array('field'=>'`uid`,`username`,`name`'));
		$this->yunset('adminUserList',$adminUserList);
		$this -> yunset('auid', $auid);
		
		include(CONFIG_PATH.'db.data.php');
	    $this->yunset('canpay',$arr_data['pay']);

        $this -> yuntpl(array('admin/crm_index'));  
    }
	 
	function addDeal_action(){
	    
	    if (!empty($_POST)){
	        
	        $crmM  =   $this->MODEL('crm');
	        
	        $data  =   $_POST;
	        
	        $data['auid']  =   $_SESSION['auid'];
	        
	        $return        =   $crmM -> addDeal($data);
	        
	    }else{
	        $return    =   array(
	            'errcode'  =>  8,
	            'msg'      =>  '操作错误，请重试！'
	        );
	    }
	    
	    echo   json_encode($return);die;
	}
	
	function searchcom_action(){
		
		if($_POST['keyword']){
			
			$companyM	=   $this -> MODEL('company');
			$keyword	=	$this->post_trim($_POST['keyword']);
			
			$list		=   $companyM -> getList(array('crm_uid'=> $_SESSION['auid'],'name'=>array('like',$keyword)),array('field' => '`uid`,`name`'));
			
			$com		=	$list['list'];
			
 			if(is_array($com) && !empty($com)){
				foreach($com as $val){
					$data[]=array('uid'=>$val['uid'],'name'=> $val['name'],); 
					
				 }
			} 
		}
		echo json_encode($data);die;
	}
	
	public function orderprice_action(){
		$id					=	intval($_POST['id']);
		$ratingM			=	$this -> MODEL('rating');
		$rating				=	$ratingM -> getInfo(array('id' => $id));
		echo json_encode($rating);
	}
	
	/**
	 * @desc 我的简报
	 */
	function getWorkReport_action(){
	    
	    $crmM      =   $this -> MODEL('crm');
	    if ($_POST['time'] == 1) {//今天
            $sDate  =   mktime(0, 0, 0, date('m'), date('d') , date('Y'));
			$eDate  =   mktime(23, 59, 59, date('m'), date('d') , date('Y'));
            
        }else if ($_POST['time'] == 2) {//昨天
            $sDate  =   mktime(0, 0, 0, date('m'), date('d'), date('Y'))  - 86400;
			$eDate  =   mktime(23, 59, 59, date('m'), date('d'), date('Y'))  - 86400;
            
        }else if ($_POST['time'] == 3) {//本周
            
            $sDate  =   strtotime(date('Y-m-d', strtotime("this week Monday", time())));
            $eDate  =   strtotime(date('Y-m-d', strtotime("this week Sunday", time()))) + 24 * 3600 - 1;
            
        }else if ($_POST['time'] == 4) {//本月
            
            $sDate  =   mktime(0, 0, 0, date('m'), 1, date('Y'));
            $eDate  =   mktime(23, 59, 59, date('m'), date('t'), date('Y'));
            
        }
	    
	    $return    =   $crmM -> getWorkReport(array('sdate' => $sDate, 'edate' => $eDate, 'auid' => $_SESSION['auid']));
 	    
 	    echo json_encode($return);
	}
	
	/**
	 * @desc  认领客户
	 */
	function receiveKh_action(){
	    
	    if ($_POST['uids']) {
	        
	        $uids  =   @explode(',', $_POST['uids']);
 	        
 	        $auid  =   intval($_SESSION['auid']);
	        
	        $comM  =   $this -> MODEL('company');
	        
	        $nid   =   $comM -> upInfo($uids, '', array('crm_uid' => $auid,'crm_time'=>time()));
	        
	        echo $nid ? 1 : 2;
	    }
	}
    
}

?>